package com.gouda.backend.controller.rbac;

import com.themis.dto.FrontFunctionDto;
import com.themis.dto.FrontUserDto;
import com.themis.dto.SystemUserDto;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import com.gouda.backend.controller.AbstractController;
import com.gouda.backend.exception.ErrorCode;
import com.gouda.core.service.systemManage.SystemUserService;
import org.amethyst.atlas.basic.component.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * Created by KGL on 2015/8/14.
 */
@Controller
@RequestMapping("/systemUser")
public class BackendSystemUserController extends AbstractController {

    @Autowired
    private SystemUserService userService;

    @RequestMapping("/getSystemUsers")
    public @ResponseBody
    Map<String, Object> getSystemUsers(HttpServletRequest request, HttpServletResponse response){
        Map<String, Object> resultMap = Maps.newHashMap();
        ErrorCode operateResult = ErrorCode.SUCCESS;

        final String loginUserId = this.getAdminUserId(request);

        String userCode = request.getParameter("userCode");
        String userName = request.getParameter("userName");
        String userStatus = request.getParameter("userStatus");
        String pageNum = request.getParameter("pageNum");

        PageData<SystemUserDto> pageDto = userService.loadUsersByPrivilege(loginUserId, userCode, userName, userStatus, pageNum);
        resultMap.put("code", operateResult.getCode());
        resultMap.put("pageModel", pageDto);
        return resultMap;
    }

    @RequestMapping("/modifyUser")
    public @ResponseBody
    Map<String, Object> modifyUser(@RequestBody FrontUserDto userDto, HttpServletRequest request){
        Map<String, Object> resultMap = Maps.newHashMap();
        ErrorCode operateResult = ErrorCode.SUCCESS;

        final String loginUserId = this.getAdminUserId(request);

        final String userId = userDto.getUserId();
        final String userName = userDto.getModifyUserName();
        final String userPassword = userDto.getModifyUserPassword();
        final String userStatus = userDto.getModifyUserStatus();
        if (Strings.isNullOrEmpty(userId) || Strings.isNullOrEmpty(loginUserId)){
            resultMap.put("code", ErrorCode.FAILED.getCode());
            resultMap.put("msg", ErrorCode.FAILED.getMessage());
            return resultMap;
        }
        if(!Strings.isNullOrEmpty(userName) || !Strings.isNullOrEmpty(userStatus) || !Strings.isNullOrEmpty(userPassword)){
            userService.modifyUser(loginUserId, userId, userName, userPassword, userStatus);
        }
        resultMap.put("code", operateResult.getCode());
        return resultMap;
    }

    @RequestMapping("/addUser")
    public @ResponseBody
    Map<String, Object> addUser(@RequestBody FrontUserDto userDto, HttpServletRequest request){
        Map<String, Object> resultMap = Maps.newHashMap();
        ErrorCode operateResult = ErrorCode.SUCCESS;

        final String userCode = userDto.getUserCodeAdd();
        final String userName = userDto.getUserNameAdd();
        if (Strings.isNullOrEmpty(userCode) || Strings.isNullOrEmpty(userName)){
            resultMap.put("code", ErrorCode.FAILED.getCode());
            resultMap.put("msg", ErrorCode.FAILED.getMessage());
            return resultMap;
        }

        //校验编码唯一性
        if(userService.checkExist(userCode)){
            resultMap.put("code", ErrorCode.EC_400005.getCode());
            resultMap.put("msg", ErrorCode.EC_400005.getMessage());
            return resultMap;
        }

        final String loginUserId = this.getAdminUserId(request);

        userService.addUser(loginUserId, userCode, userName);
        resultMap.put("code", operateResult.getCode());
        return resultMap;
    }

    @RequestMapping("/authUser")
    public @ResponseBody
    Map<String, Object> authUser(@RequestBody FrontUserDto userDto){
        Map<String, Object> resultMap = Maps.newHashMap();
        ErrorCode operateResult = ErrorCode.SUCCESS;

        final String userId = userDto.getAuthUserId();
        final String rolesStr = userDto.getRolesStr();
        if (Strings.isNullOrEmpty(userId) || Strings.isNullOrEmpty(rolesStr)){
            resultMap.put("code", ErrorCode.FAILED.getCode());
            resultMap.put("msg", ErrorCode.FAILED.getMessage());
            return resultMap;
        }

        userService.auth(userId, rolesStr);
        resultMap.put("code", operateResult.getCode());
        return resultMap;
    }

    @RequestMapping("/getUserFunctions")
    public @ResponseBody
    Map<String, Object> getUserFunctions(HttpServletRequest request, HttpServletResponse response){
        Map<String, Object> resultMap = Maps.newHashMap();
        ErrorCode operateResult = ErrorCode.SUCCESS;

        final String userId = this.getAdminUserId(request);
        if (Strings.isNullOrEmpty(userId)){
            resultMap.put("code", ErrorCode.FAILED.getCode());
            resultMap.put("msg", ErrorCode.FAILED.getMessage());
            return resultMap;
        }

        List<FrontFunctionDto> userPrivileges = userService.loadUserPrivileges(Long.valueOf(userId));
        if (null == userPrivileges) operateResult = ErrorCode.FAILED;
        resultMap.put("menu", userPrivileges);
        resultMap.put("code", operateResult.getCode());
        return resultMap;
    }

    @RequestMapping("/logout")
    public @ResponseBody
    Map<String, Object> logout(HttpServletRequest request, HttpServletResponse response){
        Map<String, Object> resultMap = Maps.newHashMap();
        ErrorCode operateResult = ErrorCode.SUCCESS;

        delCookie(response, "adminId", null);

        resultMap.put("result", operateResult.getCode());
        return resultMap;
    }
}
